/*
-------------------------------------------------------------------------------
This file is part of FlyingPaper.

Copyright (c) 2009-2010 Emilien Dupont
Also see acknowledgements in COPYING.txt

FlyingPaper is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

FlyingPaper is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with FlyingPaper. If not, see <http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------------
*/

/**
 * \file flyingPaper.cpp
 * \brief FlyingPaper's methods definition
 * \author Emilien Dupont
 *
 * This file contains the definition of the FlyingPaper's methods.
 */

#include "flyingPaper.h"

#define DEL(T)     \
if(T)              \
    delete(T)      \

FlyingPaper::FlyingPaper()
{
    _Root = 0;
    _SceneManager = 0;
    _RenderWindow = 0;
    _Viewport = 0;
    _Camera = 0;

    _InputManager = 0;
    _Keyboard = 0;
    _Mouse = 0;

    _GUIRenderer = 0;
    _GUISystem = 0;

    _StateManager = 0;
    _MenuState = 0;
    _GameState = 0;
    _CreditsState = 0;
    _GameOverState = 0;
}

FlyingPaper::~FlyingPaper()
{
    stop();
}

void FlyingPaper::start()
{
    initOgre();
    initOIS();
    initCEGUI();

    _Root->startRendering();
}

void FlyingPaper::stop()
{
    DEL(_StateManager);
    DEL(_GUISystem);
    DEL(_GUIRenderer);
    DEL(_Root);
}

void FlyingPaper::initOgre()
{
    _Root = new Ogre::Root();
    if(!_Root->restoreConfig() && !_Root->showConfigDialog())
		std::exit(0);

    loadResources();

    _RenderWindow = _Root->initialise(true,"Flying Paper");
	_SceneManager = _Root->createSceneManager(Ogre::ST_GENERIC, "SceneManager");

	_Camera = _SceneManager->createCamera("PlayerCamera");
	_Camera->setNearClipDistance(1);

	if (_Root->getRenderSystem()->getCapabilities()->hasCapability(Ogre::RSC_INFINITE_FAR_PLANE))
        _Camera->setFarClipDistance(0);
	else
		_Camera->setFarClipDistance(5000);

	_Viewport = _RenderWindow->addViewport(_Camera);

	_SceneManager->setAmbientLight(Ogre::ColourValue(255, 255, 255));


}

void FlyingPaper::loadResources()
{
    Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../Media/","FileSystem","General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../Media/GUI/","FileSystem","General");
	Ogre::ResourceGroupManager::getSingleton().addResourceLocation("../Media/Models/","FileSystem","General");

	Ogre::ResourceGroupManager::getSingleton().initialiseAllResourceGroups();
}

void FlyingPaper::initOIS()
{
    size_t windowHnd = 0;
    std::ostringstream windowHndStr;
    OIS::ParamList pl;
	Ogre::RenderWindow *win = _Root->getAutoCreatedWindow();

    win->getCustomAttribute("WINDOW", &windowHnd);
    windowHndStr << windowHnd;
    pl.insert(std::make_pair(std::string("WINDOW"), windowHndStr.str()));
    _InputManager = OIS::InputManager::createInputSystem(pl);

    _Keyboard = static_cast<OIS::Keyboard*>(_InputManager->createInputObject(OIS::OISKeyboard, true));
    _Mouse = static_cast<OIS::Mouse*>(_InputManager->createInputObject(OIS::OISMouse, true));
}

void FlyingPaper::initCEGUI()
{
    _GUIRenderer = new CEGUI::OgreCEGUIRenderer(_RenderWindow, Ogre::RENDER_QUEUE_OVERLAY, false, 3000, _SceneManager);
	_GUISystem = new CEGUI::System(_GUIRenderer);

	CEGUI::Logger::getSingleton().setLoggingLevel(CEGUI::Insane);
	CEGUI::Logger::getSingleton().setLogFilename("cegui.log");
}

void FlyingPaper::initStates()
{

}
